package com.edu.app.controller.core;

import com.alibaba.fastjson.JSON;
import com.edu.app.bean.core.SysOpty;
import com.edu.app.bean.virtualbean.SelectedItems;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysOptyService;
import com.edu.app.service.sys.linkservices.SysUserActivityService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/opty")
@RestController
public class SysOptyController {

    @Autowired
    private SysOptyService sysOptyService;

    @Autowired
    private SysUserActivityService sysUserActivityService;

    @RequestMapping(value = "")
    @RequiresAuthentication
    public ModelMap getOptyList (){

        try{
            List<SysOpty> permissionList = sysOptyService.getObjectList();
            return MsgUtil.retMsg(SysMessage.GET_OPTY_SUCCEED, StaticConstant.OPPORTUNITY_LIST, permissionList);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping(value = "/{id}")
    @RequiresAuthentication
    public ModelMap getOptyById(@PathVariable Integer id){

        try{
            SysOpty sysOpty  = sysOptyService.getObjectById(id);
            return MsgUtil.retMsg(SysMessage.GET_OPTY_SUCCEED, StaticConstant.OPPORTUNITY, sysOpty);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @GetMapping("/pages")
    @RequiresAuthentication
    public ModelMap getOptyByPage(@RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysOptyService.findItemByPage(offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @GetMapping("/pages/optyModel")
    @RequiresAuthentication
    public ModelMap getOptyModelByPage(@RequestParam Integer user_id, @RequestParam Integer offset, @RequestParam Integer limit) {

        try {
            PageInfo pageInfo = sysOptyService.getOptyModelByPage(user_id, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_BY_PAGE_SUCCEED, "page_info", pageInfo);
        } catch (Exception e) {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addOpty(@RequestHeader("Authorization") String token, @RequestBody SysOpty sysOpty, @RequestParam String employeeList, @RequestParam String customerList){

        try{
            String username = JWTUtil.getUsername(token);
            sysOpty.initializeSystemColumns(username);

            List<SelectedItems> selectedItems = JSON.parseArray(employeeList, SelectedItems.class);
            if(selectedItems.size() > 0){
                sysOpty.setUser_id(selectedItems.get(0).getValue());
            }

            selectedItems = JSON.parseArray(customerList, SelectedItems.class);
            if(selectedItems.size() > 0){
                sysOpty.setCustomer_id(selectedItems.get(0).getValue());
            }

            Integer r = sysOptyService.add(sysOpty);

            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_OPTY_SUCCEED, null, null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.OPTY_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }

    }

    @PutMapping(value = "")
    @RequiresAuthentication
    public ModelMap update (@RequestHeader("Authorization") String token, @RequestBody SysOpty sysOpty){

        String username = JWTUtil.getUsername(token);
        sysOpty.initializeSystemColumns(username);

        Integer i = sysOptyService.update(sysOpty);

        if(i > 0 ){
            return MsgUtil.retMsg(SysMessage.UPDATE_OPTY_SUCCEED, null, null);
        }
        else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }

    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap delete (@RequestParam Integer id){

        ModelMap map = new ModelMap();
        Integer i = sysOptyService.delete(id);
        if(i > 0 )
        {
            //continue to delete the intersection table related with current activity

            return MsgUtil.retMsg(SysMessage.DELETE_OPTY_SUCCEED, null, null);
        }
        else
        {
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }

    }
}
